import 'dart:math';

import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert';
import 'package:flutter_shop_z/model/commodity.dart';
import 'package:flutter_shop_z/model/commodity_cart.dart';

class CommodityCartPrefs {
  static String cartListKey = "cartListKey";

  //保存商品
  static void saveCommodityCart(Commodity commodity) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String listStr = prefs.getString(cartListKey);
    List jsonListCart = [];
    List<CommodityCart> listCart = [];
    if (listStr != null) {
      jsonListCart = jsonDecode(listStr);
      bool isExistCommodity = false;
      if (jsonListCart.length > 0) {
        jsonListCart.forEach((value) {
          if (value != null) {
            CommodityCart cart = CommodityCart.fromJson(value);
            print("cart.commodityName::===>${cart.commodityName}");
            if (cart.commodityId == commodity.commodityId) {
              isExistCommodity = true;
              cart.isCheck = true;
              cart.number++;
            }
            listCart.add(cart);
          }
        });
      }
      if (!isExistCommodity) {
        CommodityCart cart = CommodityCart.toCart(commodity);
        listCart.add(cart);
      }

      String listCartStr = jsonEncode(listCart);
      prefs.setString(cartListKey, listCartStr);
    } else {
      CommodityCart commodityCart = CommodityCart.toCart(commodity);
      listCart.add(commodityCart);
      String listCartStr = jsonEncode(listCart);
      prefs.setString(cartListKey, listCartStr);
    }
  }

  //修改选中状态
  static void checkCartState(CommodityCart commodityCart) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String listStr = sharedPreferences.getString(cartListKey);
    List<CommodityCart> jsonListCart = _decodeList(listStr);
    jsonListCart.forEach((element) {
      if (element.commodityId == commodityCart.commodityId) {
        element.isCheck = commodityCart.isCheck;
      }
    });
    //保存
    String listCartStr = jsonEncode(jsonListCart);
    sharedPreferences.setString(cartListKey, listCartStr);
  }

  //修改全部选中状态
  static void checkAllCartState(bool isCheck) async {
    print("isCheck===>${isCheck}");
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String listStr = sharedPreferences.getString(cartListKey);
    List<CommodityCart> jsonListCart = _decodeList(listStr);
    jsonListCart.forEach((element) {
      element.isCheck = isCheck;
    });
    //保存
    String listCartStr = jsonEncode(jsonListCart);
    sharedPreferences.setString(cartListKey, listCartStr);
  }

//清空购物车商品
  static void clearCommodityCart() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    preferences.remove(cartListKey);
  }

  //清空选中的商品
  static void clearisCheckCommodityCart() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String listStr = sharedPreferences.getString(cartListKey);
    List<CommodityCart> jsonListCart = _decodeList(listStr);
    jsonListCart.removeWhere((element) => element.isCheck);
    //不能直接操作拿出来的数据
    // List<CommodityCart> jsonListCart2 = jsonListCart;
    // jsonListCart2.forEach((element) {
    //   if (element.isCheck) {
    //     jsonListCart2.remove(element);
    //   }
    // });
    //保存
    String listCartStr = jsonEncode(jsonListCart);
    sharedPreferences.setString(cartListKey, listCartStr);
  }

  //减少商品数量
  static void deleteCommodityNum(int commodityId) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    String listStr = preferences.getString(cartListKey);
    List<CommodityCart> listCart = _decodeList(listStr);
    List<CommodityCart> list = listCart;

    list.forEach((value) {
      if (value.commodityId == commodityId) {
        if (value.number > 1) {
          value.number--;
        }
      }
    });
    String listCartStr = jsonEncode(listCart);
    preferences.setString(cartListKey, listCartStr);
  }

  //删除商品
  static void deleteCommodity(int commodityId) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    String listStr = preferences.getString(cartListKey);
    List<CommodityCart> listCart = _decodeList(listStr);
    listCart.removeWhere(
        (commodityCart) => commodityCart.commodityId == commodityId);
    // list.forEach((value) {
    //   if (value.commodityId == commodityId) {
    //     list.remove(value);
    //   }
    // });
    String listCartStr = jsonEncode(listCart);
    preferences.setString(cartListKey, listCartStr);
  }

//将Json转List<CommodityCart>
  static List<CommodityCart> _decodeList(String json) {
    List<CommodityCart> commodityList = [];
    if (json != null) {
      List list = jsonDecode(json);
      if (list.length > 0) {
        list.forEach((element) {
          if (element != null) {
            CommodityCart cart = CommodityCart.fromJson(element);
            commodityList.add(cart);
          }
        });
      }
    } else {
      //告诉界面为空
      commodityList = [];
    }
    return commodityList;
  }

  //将Json转List<CommodityCart>
  static List<CommodityCart> decodeList(String json) {
    List<CommodityCart> commodityList = [];
    if (json != null) {
      List list = jsonDecode(json);
      if (list.length > 0) {
        list.forEach((element) {
          if (element != null) {
            CommodityCart cart = CommodityCart.fromJson(element);
            commodityList.add(cart);
          }
        });
      }
    } else {
      //告诉界面为空
      commodityList = [];
    }
    return commodityList;
  }
}
